<?php
/**
 * 管理中心· 商品管理模块的逻辑处理
 * ============================================================================
 * 版权所有 2017-2020 北京优特世纪科技有限公司，并保留所有权利。
 * 网站地址: http://www.unitewww.com;
 * ============================================================================
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @since: 1.0
 * @version: 1.0
 **/
namespace Hoteladmin\Controller;
use Hoteladmin\Model\GoodsModel;
use Think\AjaxPage;
class GoodsController extends BaseController {
    //初始化程序
    public function _initialize()
    {
        parent::_initialize();
    
        $timegap['beg'] = date('Y-m-d',strtotime("-1 year"));//1年前
        $timegap['end'] = date('Y-m-d',strtotime("+1 days"));
        $this->assign('timegap',$timegap);
    }
/*************************** 普通商品相关信息 ***************************************/
	/**
	 +--------------------------------------------------------------------------
	 * 实现获取普通商品列表的功能
	 * @author:  XC跃
	 * @date :2018-09-28
	 +--------------------------------------------------------------------------
	 */
	public function goodsList(){
	    $this->display();
	}
	public function ajaxGoodsList(){
	    //实例化相关表
	    $goident = M('goods');//酒店房间列表
	    $gcident = M('category');//酒店名称
	    $roident = M('rtype');//房间类型
	    // 搜索条件
	    $condition = array();
			$condition['go_type'] = '1';
	    I('is_on_sale') ? $condition['go_sale'] = I('is_on_sale') : false;
	    I('intro') ? $condition[I('intro')] = array('eq','1') : false;
	    I('keyword') ? $condition[I('xcinput')] = array('LIKE',"%".I('keyword')."%") : false;
	    //dump($condition);
	    $sort_order = I('order_by').' '.I('sort');
	    //echo $sort_order;
	    $count = $goident->where($condition)->count();
	    $Page  = new AjaxPage($count,50);
	    $show = $Page->show();
	    $list = $goident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
	    //echo $goident->getLastSql();
	    $this->assign('list',$list);
	    $this->assign('page',$show);// 赋值分页输出
	    $this->assign('rows',$Page->totalRows);
	    $this->assign('cate',$gcident->getField('ca_id,ca_name'));
	    $this->assign('room',$roident->getField('ro_rid,ro_room'));
	    $this->display();
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现添加普通商品信息的功能
	 * @author:  XC跃
	 * @date :2018-09-28
	 +--------------------------------------------------------------------------
	 */
	public function goodsAddEdit(){

	    $goid = I('id/d');
	    //实例化相关表
	    $goident = M('goods');
	   // $brident = M('brand');
	    $caident = M('category');
	    $giident = M('goods_images');
	    $roident = M('rtype');
	    $are = M('area');
		//实例化相关Model
		$goidentr = new GoodsModel();
		if(IS_POST){
			$re = $goidentr->goods_add_edit($goid);
			$res= array('status'=>1,'msg'=>'操作成功');
			exit(json_encode($res));
		}else{
			//获取数据
		    $caList = $caident->order("ca_parent asc")->select();
		    $roList = $roident->select();
			//$brList = $brident->where("`br_state`='1'")->order('br_recom asc,br_id desc')->select();
			if(''!=$goid){
			    $list = $goident->where("`go_id`='".$goid."'")->find();
			    $imgs = $giident->where("`im_gid`='".$goid."'")->order('im_sort')->select();
			    if(count($list)=='0'){
			        layer_error('参数错误');
			    }
                $parent_id['re_parent_id'] = $list['go_pro'];
                $shi = M('area')->where($parent_id)->select();
                $parent_id['re_parent_id'] = $list['go_shi'];
                $xian = M('area')->where($parent_id)->select();
			    $this->assign('shi',$shi);
			    $this->assign('xian',$xian);
			    $this->assign('list',$list);
			    $this->assign('type',2);

			    $this->assign('imgs',$imgs);
			}else{
                $this->assign('type',1);
            }
			$region = M('area')->where(array('re_parent_id'=>0))->select();

			$this->assign('calist',tree(merg($caList)));
			$this->assign('region',$region);
			$this->assign('rolist',$roList);
			//$this->assign('brlist',$brList);
			$this->assign('click',rand(10,199));
			$this->display();
		}
	}
	public function addres(){
        $parent_id['re_parent_id'] = I('post.pro_id');
        $region = M('area')->where($parent_id)->select();
        $opt = '<option>--请选择市区--</option>';
        foreach($region as $key=>$val){
            $opt .= "<option value='{$val['re_id']}'>{$val['re_name']}</option>";
        }
        echo json_encode($opt);
    }
	/*************************** 积分商品相关信息 ***************************************/
	/**
	+--------------------------------------------------------------------------
	 * 实现获取积分商品列表的功能
	 * @author:  XC跃
	 * @date :2018-09-28
	+--------------------------------------------------------------------------
	 */
	public function pointsList(){
		$this->display();
	}
	public function ajaxPointsList(){
		//实例化相关表
		$goident = M('goods');
		$gcident = M('category');
		// 搜索条件
		$condition = array();
		$condition['go_type'] = '2';
		I('is_on_sale') ? $condition['go_sale'] = I('is_on_sale') : false;
		I('intro') ? $condition[I('intro')] = array('eq','1') : false;
		I('keyword') ? $condition[I('xcinput')] = array('LIKE',"%".I('keyword')."%") : false;
		//dump($condition);
		$sort_order = I('order_by').' '.I('sort');
		//echo $sort_order;
		$count = $goident->where($condition)->count();
		$Page  = new AjaxPage($count,50);
		$show = $Page->show();
		$list = $goident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
		//echo $goident->getLastSql();
		$this->assign('list',$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('rows',$Page->totalRows);
		$this->assign('cate',$gcident->getField('ca_id,ca_name'));
		$this->display();
	}
	/**
	+--------------------------------------------------------------------------
	 * 实现添加积分商品信息的功能
	 * @author:  XC跃
	 * @date :2018-09-28
	+--------------------------------------------------------------------------
	 */
	public function pointsAddEdit(){
		$goid = I('id/d');
		//实例化相关表
		$goident = M('goods');
		$caident = M('category');
		$giident = M('goods_images');
		//实例化相关Model
		$goidentr = new GoodsModel();
		if(IS_POST){
			$re = $goidentr->points_add_edit($goid);
			$res= array('status'=>1,'msg'=>'操作成功');
			exit(json_encode($res));
		}else{
			//获取数据
			$caList = $caident->order("ca_parent asc")->select();
			//$brList = $brident->where("`br_state`='1'")->order('br_recom asc,br_id desc')->select();
			if(''!=$goid){
				$list = $goident->where("`go_id`='".$goid."'")->find();
				$imgs = $giident->where("`im_gid`='".$goid."'")->order('im_sort')->select();
				if(count($list)=='0'){
					layer_error('参数错误');
				}
				$this->assign('list',$list);
				$this->assign('imgs',$imgs);
			}
			$this->assign('calist',tree(merg($caList)));
			$this->assign('click',rand(10,199));
			$this->display();
		}
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现商品的多功能操作
	 * @author:XC跃 
	 * @date: 2018-09-28
	 +--------------------------------------------------------------------------
	 */
	public function batch_process(){
	    //接收相关值
	    $type = I('post.type');
	    $ids  = I('post.selected');
	    //实例化相关表
	    $gident = M('Goods');
	    if(!in_array($type,array('sale1','sale2','recom1','recom2','new1','new2','hot1','hot2')) || !$ids){
	        $res = array('status'=>-1,'msg'=>'非法操作');
	        exit(json_encode($res));
	    }
	    $where = "`go_id` in (".$ids.")";
	    $field = "go_".substr($type,0,-1);
	    $value = substr($type,-1);
	    $row = $gident->where($where)->save(array($field=>$value));
	    if($row>0){
	        $res = array('status'=>1,'msg'=>'批量处理成功');
	    }else{
	        $res = array('status'=>-1,'msg'=>'批量处理失败');
	    }
	    exit(json_encode($res));
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现商品删除的功能
	 * @author：XC跃
	 * @date 2018-09-28
	 +--------------------------------------------------------------------------
	 */
	public function delGoods(){
	    $id =  trim(I('id/d'));
	    //实例化相关表
	    $goident = M('goods');
	    $giident = M('goods_images');
	    //获得商品信息
	    $goFind = $goident->where("`go_id`='".$id."'")->find();
	    if(count($goFind)=='0'){
	        $res = array('status'=>-1,'msg'=>'参数错误');
	        exit(json_encode($res));
	    }
	    //删除此商品
	    delPath("goods","go_id=$id","go_thumb");   //清除图片
	    $goident->where("`go_id`='".$id."'")->delete();
	    //删除效果图
	    $list = $giident->where("im_gid=".$id)->select();
	    foreach($list as $key=>$val){
	        $path = ".".$val['im_url'];
	        if (file_exists($path)){
	            unlink($path);
	        }
	        $giident->where("im_gid=".$id)->delete();
	    }
	    exit(json_encode(array('status'=>1)));
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现删除商品图片的功能
	 * @author：XC跃
	 * @date 2018-09-28
	 +--------------------------------------------------------------------------
	 */
	public function delImg(){
	    //接收相关值
	    $id = trim(I('id/d'));
	    //实例化相关表
	    $gident = M('goods_images');
	    $list=$gident->where("im_id=".$id)->find();
	    $path = "./Public/Uploads/".$list['im_url'];
	    if (file_exists($path)){
	        unlink($path);
	    }
	    $gident->where("im_id=".$id)->delete();
	    exit();
	}
    /**
    +--------------------------------------------------------------------------
     * 实现删除商品图片的功能
     * @author：XC跃
     * @date 2018-09-28
    +--------------------------------------------------------------------------
     */
    public function de(){
        //接收相关值
        $id = trim(I('id/d'));
        //实例化相关表
        $gident = M('daili_images');
        $list=$gident->where("im_id=".$id)->find();
        $path = "./Public/Uploads/".$list['im_url'];
        if (file_exists($path)){
            unlink($path);
        }
        $gident->where("im_id=".$id)->delete();
        exit();
    }
    /**
    +--------------------------------------------------------------------------
     * 实现删除商品图片的功能
     * @author：XC跃
     * @date 2018-09-28
    +--------------------------------------------------------------------------
     */
    public function del(){
        //接收相关值
        $id = trim(I('id/d'));
        //实例化相关表
        $gident = M('daili_images');
        $list=$gident->where("im_id=".$id)->find();
        $path = "./Public/Uploads/".$list['im_url'];
        if (file_exists($path)){
            unlink($path);
        }
        $gident->where("im_id=".$id)->delete();
        exit();
    }
/*************************** 商品分类相关信息 ***************************************/
    /**
     +--------------------------------------------------------------------------
     * 实现获取酒店分类列表的功能
     *
     +--------------------------------------------------------------------------
     */
    public function cateList(){
        //实例化相关表
        $caident = M('category');
    	//获取数据
    	$caList = $caident->order("ca_sort asc")->select();
    	$this->assign('calist',tree(merg($caList)));
    	$this->display();
    }
    /**
     +--------------------------------------------------------------------------
     * 实现商品分类列表的添加/修改
     * @author:  XC跃
     * @date :2018-10-10
     +--------------------------------------------------------------------------
     */
    public function cateAddEdit(){
        $caid = I('cid/d');
        //实例化相关Model
        $goidentr = new GoodsModel();
        
        if(IS_POST){
            $re = $goidentr->cate_add_edit($caid);
            if($re>0){
                $res = array('status'=>1,'msg'=>'保存成功');
            }else{
                $res = array('status'=>-1,'msg'=>'保存失败');
            }exit(json_encode($res));
        }else{
            $caList = $goidentr->getCateParent();
            if(''!=$caid){
                $caFind = $goidentr->getCateFind($caid);
                $this->assign('re',$caFind);
                //dump($brList);
            }
            $this->assign('calist',$caList);
            $this->display();
        }
    }
    /**
     +--------------------------------------------------------------------------
     * 实现删除商品分类列表
     * @author:  XC跃
     * @date :2018-10-10
     +--------------------------------------------------------------------------
     */
    public function cateDel(){
    	$cid = I('post.id');
    	//实例化相关表
    	$goident = M('goods');
    	$caident = M("category");
    	$caFind = $caident->where("`ca_id`='".$cid."'")->find();
    	if($caFind['ca_parent']=='0'){
    	    $cou = $caident->where("`ca_parent`='".$cid."'")->count();
    		if($cou>0){
    			$res = array('status'=>-1,'msg'=>'当前分类下面存在子分类，请先将子分类移除');
    			exit(json_encode($res));
    		}
    	}
    	//统计商品列表是否包含此分类
    	$gcou = $goident->where("`go_caid`='".$cid."'")->count();
    	if($gcou>0){
    		$res = array('status'=>-1,'msg'=>'当前分类下面存在商品，请先将商品移动到别的分类');
    		exit(json_encode($res));
    	}
    	$re = $caident->where("`ca_id`='".$cid."'")->delete();
    	if($re>0){
    		$res = array('status'=>1,'msg'=>'删除成功');
    		exit(json_encode($res));
    	}
    }

    /**
     * 获取房间类型列表
     */
    public function roomList(){
        //实例化相关表
        $caident = M('rtype');
        //获取数据
        $roList = $caident->select();
        $this->assign('rolist',$roList);
        $this->display();
    }

    /**
    +--------------------------------------------------------------------------
     * 实现房间分类列表的添加/修改
     * @author:  XC跃
     * @date :2018-10-10
    +--------------------------------------------------------------------------
     */
    public function roomAddEdit(){
        $roid = I('rid');
//        echo "<pre>";
////        print_r($roid);
////        die;
        //实例化相关Model
        $goidentr = new GoodsModel();

        if(IS_POST){
            $re = $goidentr->room_add_edit($roid);
            if($re>0){
                $res = array('status'=>1,'msg'=>'保存成功');
            }else{
                $res = array('status'=>-1,'msg'=>'保存失败');
            }exit(json_encode($res));
        }else{
            $roList = $goidentr->getRoomParent();
            if(''!=$roid){
                $caFind = $goidentr->getRoomFind($roid);
                $this->assign('re',$caFind);
                //dump($brList);
            }
            $this->assign('rolist',$roList);
            $this->display();
        }
    }
    /**
    +--------------------------------------------------------------------------
     * 实现删除房间分类列表
     * @author:  XC跃
     * @date :2018-10-10
    +--------------------------------------------------------------------------
     */
    public function roomDel(){
        $rid = I('post.id');
        //实例化相关表
        $goident = M('goods');
        $roident = M("rtype");
//        $caFind = $caident->where("`ca_id`='".$rid."'")->find();
//        if($caFind['ca_parent']=='0'){
//            $cou = $caident->where("`ca_parent`='".$rid."'")->count();
//            if($cou>0){
//                $res = array('status'=>-1,'msg'=>'当前分类下面存在子分类，请先将子分类移除');
//                exit(json_encode($res));
//            }
//        }
        //统计房间列表是否包含此分类
        $gcou = $goident->where("`go_rid`='".$rid."'")->count();
        if($gcou>0){
            $res = array('status'=>-1,'msg'=>'当前分类下面存在房间，请先将房间类别移动到别的分类');
            exit(json_encode($res));
        }
        $re = $roident->where("`ro_rid`='".$rid."'")->delete();
        if($re>0){
            $res = array('status'=>1,'msg'=>'删除成功');
            exit(json_encode($res));
        }
    }

    /*************************** 兑换商品相关信息 ***************************************/
    /**
     +--------------------------------------------------------------------------
     * 实现获取兑换商品列表的功能
     * @author:  XC跃
     * @date :2018-04-08
     +--------------------------------------------------------------------------
     */
	public function propList(){
		$this->display();
	}
	public function ajaxPropList(){
		//实例化相关表
		$gpident = M('prop');
		$grident = M('grade');
		// 搜索条件
		$condition = array();
		I('is_on_sale') ? $condition['gp_sale'] = I('is_on_sale') : false;
		I('gname') ? $condition['gp_name'] = array('LIKE',"%".I('gname')."%") : false;
		$sort_order = I('order_by').' '.I('sort');
		//echo $sort_order;
		$count = $gpident->where($condition)->count();
		$Page  = new AjaxPage($count,50);
		$show = $Page->show();
		$list = $gpident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($list as $key=>$val){
			$list[$key]['grade'] = $grident->where("`gr_id`='".$val['gp_gid']."'")->getField('gr_name');
		}

//		dump($list);
//				dump($grFind);
		//echo $gpident->getLastSql();
		$this->assign('list',$list);

		$this->assign('page',$show);// 赋值分页输出
		$this->assign('rows',$Page->totalRows);
		$this->display();
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现添加兑换商品信息的功能
	 * @author:XC跃
	 * @date :2018-04-08
	 +--------------------------------------------------------------------------
	 */
	public function propAddEdit(){
		//接收传值
		$goid = I('id/d');
		//实例化相关表
		$gpident = M('prop');
		$grident = M('grade');
		$grFind = $grident->select();
//		dump($grFind);
		if(IS_POST){
		    //接收传值
			$data['gp_gid']   = trim(I('post.gid'));
			$data['gp_name']  = trim(I('post.pname'));
			$data['gp_thumb'] = trim(I('post.thumbImgUrl'));
			$data['gp_money'] = trim(I('post.money'));
			$data['gp_price'] = trim(I('post.price'));
			if(''!=$goid){
					$re = $gpident->where("`gp_id`='".$goid."'")->save($data);
			}else{
					$data['gp_nums'] = rand(100,400);
					$data['gp_atime']= time();
					$re = $gpident->add($data);
			}
			if($re>0){
					$res= array('status'=>1,'msg'=>'保存成功');
			}else{
					$res= array('status'=>-1,'msg'=>'保存失败');
			}exit(json_encode($res));
		}else{
			if(''!=$goid){
				$list = $gpident->where("`gp_id`='".$goid."'")->find();
				if(count($list)=='0') layer_error('参数错误');
				$this->assign('res',$list);
			}
			$this->assign('grFind',$grFind);
			$this->display();
		}
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现删除道具商品信息的功能
	 * @author：XC跃
	 * @date 2018-09-28
	 +--------------------------------------------------------------------------
	 */
	public function delProp(){
		$id = trim(I('id/d'));
		//实例化相关表
		$prident = M('prop');
		//获得商品信息
		$goFind = $prident->where("`gp_id`='".$id."'")->find();
		//dump($goFind);
		if(count($goFind)=='0'){
			$res = array('status'=>-1,'msg'=>'参数错误');
			exit(json_encode($res));
		}
		//删除此商品
		delPath("prop","gp_id=$id","gp_thumb");   //清除图片
		$prident->where("`gp_id`='".$id."'")->delete();
		exit(json_encode(array('status'=>1)));
	}
    /**
    +--------------------------------------------------------------------------
     * 实现删除道具商品信息的功能
     * @author：XXT
     * @date 2019-11-12
    +--------------------------------------------------------------------------
     */
    public function delIm(){
        $id = trim(I('id/d'));
        //实例化相关表
        $prident = M('daili_goods');
        //获得商品信息
        $goFind = $prident->where("`dg_id`='".$id."'")->find();
        //dump($goFind);
        if(count($goFind)=='0'){
            $res = array('status'=>-1,'msg'=>'参数错误');
            exit(json_encode($res));
        }
        //删除此商品
//        delPath("prop","gp_id=$id","gp_thumb");   //清除图片
        $prident->where("`dg_id`='".$id."'")->delete();
        exit(json_encode(array('status'=>1)));
    }
	/*************************** 兑换商品相关信息 ***************************************/
	/**
	+--------------------------------------------------------------------------
	 * 实现获取批发商品列表的功能
	 * @author:  XXT
	 * @date :2019-11-14
	+--------------------------------------------------------------------------
	 */
	public function dailiList(){
		$this->display();
	}
	public function ajaxDailiList(){
        //实例化相关表
        $goident = M('daili_goods');
        // 搜索条件
        $condition = array();
        I('gname') ? $condition['dg_name'] = array('LIKE',"%".I('gname')."%") : false;//dump($condition);
        $sort_order = I('order_by').' '.I('sort');
        //echo $sort_order;
        $count = $goident->where($condition)->count();
        $Page  = new AjaxPage($count,50);
        $show = $Page->show();
        $list = $goident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
        //echo $goident->getLastSql();
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('rows',$Page->totalRows);
        $this->display();
	}
	/**
	+--------------------------------------------------------------------------
	 * 实现添加批发商品信息的功能
	 * @author:XXT
	 * @date :2019-11-14
	+--------------------------------------------------------------------------
	 */
	public function dailiAddEdit(){
		//接收传值
		$goid = I('id/d');
		//实例化相关表
		$dgident = M('daili_goods');
        if(IS_POST){
			//接收传值
			$data['dg_name']  = trim(I('post.dg_name'));
			$data['dg_thumb'] = trim(I('post.thumbImgUrl'));
			$data['dg_money'] = trim(I('post.dg_money'));
			$data['dg_price'] = trim(I('post.dg_price'));
			$data['dg_color'] = trim(I('post.dg_color'));
			$data['dg_size'] = trim(I('post.dg_size'));
			$data['dg_con'] = trim(I('post.dg_con'));
			$data['dg_body'] = trim(I('post.dg_body'));
			$data['dg_sale'] = trim(I('post.dg_sale'));
            $goods = new GoodsModel();
			if(''!=$goid){
                $data['dg_stime']= time();
                $re = $dgident->where("`dg_id`='".$goid."'")->save($data);
                $goods->daili_img_sn($goid);

			}else{
				$data['dg_stime']= time();
				$re = $dgident->add($data);
                $goods->daili_img_sn($re);
			}
            if($re>0){
				$res= array('status'=>1,'msg'=>'保存成功');
			}else{
				$res= array('status'=>-1,'msg'=>'保存失败');
			}exit(json_encode($res));
		}else{
			if(''!=$goid){
				$list = $dgident->where("`dg_id`='".$goid."'")->find();
				if(count($list)=='0') layer_error('参数错误');
                $imgs = M('daili_images')->where("`im_gid`='".$goid."'")->order('im_sort')->select();
                $this->assign('imgs',$imgs);

                $this->assign('list',$list);
			}
			$this->display();
		}
	}
	/**
	+--------------------------------------------------------------------------
	 * 实现删除道具商品信息的功能
	 * @author：XC跃
	 * @date 2018-09-28
	+--------------------------------------------------------------------------
	 */
	public function delDaili(){
		$id = trim(I('id/d'));
		//实例化相关表
		$dgident = M('daili_goods');
		//获得商品信息
		$goFind = $dgident->where("`dg_id`='".$id."'")->find();
		//dump($goFind);
		if(count($goFind)=='0'){
			$res = array('status'=>-1,'msg'=>'参数错误');
			exit(json_encode($res));
		}
		//删除此商品
		delPath("prop","dg_id=$id","dg_thumb");   //清除图片
		$dgident->where("`dg_id`='".$id."'")->delete();
		exit(json_encode(array('status'=>1)));
	}
}